#include "link_list.h"

struct link_list *CreateList(void)
{
  struct link_list *head = NULL;
  malloc(sizeof(struct link_list));
  memset(head, 0, sizeof(struct link_list));

  head->next = NULL;
  return head;
};

int insert_to_head(struct link_list *head, struct datazone datas)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return -1;
  }

  struct link_list *temp = (struct link_list *)malloc(sizeof(struct link_list));
  memset(temp, 0, sizeof(struct link_list));

  // memcpy(temp->data, &datas, sizeof(struct datazone));
  temp->data = datas;
  temp->next = head->next;
  head->next = temp;

  return 0;
}

int insert_to_end(struct link_list *head, struct datazone datas)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return -1;
  }

  struct link_list *tmp = head;
  struct link_list *temp = (struct link_list *)malloc(sizeof(struct link_list));
  memset(temp, 0, sizeof(struct link_list));
  // memcpy(temp->data, &datas, sizeof(struct datazone));
  temp->data = datas;
  temp->next = NULL;
  // 找到末尾位置
  while (tmp->next != NULL)
  {
    tmp = tmp->next;
  }

  tmp->next = temp;
  return 0;
}
/*
struct datazone get_data_in_head(struct link_list *head, struct datazone datas)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return ;
  }
  if (head->next == NULL)
  {
    printf("无数据\n");
    return ;
  }
  struct datazone datas;
  memset(datas, 0, sizeof(struct datazone));
  struct link_list *temp = head->next;

  memcpy(datas, temp->data, sizeof(struct datazone));

  return datas;
}

struct datazone get_data_in_end(struct link_list *head, struct datazone datas)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return ;
  }
  struct datazone datas;
  memset(&datas,0,sizeof(struct datazone));

  struct link_list *temp = head->next;
  if(temp->next == NULL)
  {
    return ;
  }
  while(temp->next != NULL)
  {
    temp = temp->next;
  }
  memcpy(&datas,temp->data,sizeof(struct datazone));

  return datas;
}
*/
int del_first_node(struct link_list *head)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return -1;
  }

  struct link_list *temp = head->next;
  if (temp != NULL)
  {
    head->next = temp->next;
    free(temp);
  }
  temp = NULL;

  return 0;
}

int del_end_node(struct link_list *head)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return -1;
  }

  struct link_list *temp = head->next;
  if (temp != NULL)
  {
    while (temp->next != NULL)
    {
      temp = temp->next;
    }
    head->next = temp->next;
    free(temp);
  }
  temp = NULL;

  return 0;
}

int link_list_free(struct link_list *head)
{
  if (head == NULL)
  {
    printf("头节点为空\n");
    return -1;
  }

  struct link_list *end = head;
  struct link_list *temp = head;

  while (end != NULL)
  {
    end = end->next;
    free(temp);
    temp = end;
  }
  end = NULL;
  temp = NULL;
  head = NULL;

  return 0;
}
